package com.example.hls_homework2;

import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.Timer;
import java.util.TimerTask;

public class UC3Activity extends AppCompatActivity {

    private ImageView imageView;
    private int currentImage = 1;
    private final int[] imageResources = {
            R.drawable.image1,  // 请替换为您的图片资源
            R.drawable.image2,
            R.drawable.image3,
            R.drawable.image4,
            R.drawable.image5
    };
    private Timer timer;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uc3);

        imageView = findViewById(R.id.image_view);
        findViewById(R.id.btn_back).setOnClickListener(v -> finish());
    }

    @Override
    protected void onResume() {
        super.onResume();
        startImageRotation();
    }

    @Override
    protected void onPause() {
        super.onPause();
        stopImageRotation();
    }

    private void startImageRotation() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(() -> {
                    imageView.setImageResource(imageResources[currentImage]);
                    currentImage = (currentImage + 1) % imageResources.length;
                });
            }
        }, 0, 2000); // 每2秒切换一次图片
    }

    private void stopImageRotation() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }
}